﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace Projekt_GUI.Forms
{
    public partial class KundeÜbersicht : Form
    {
        OracleHelper oracle { get { return OracleHelper.Instance; } }

        public enum sorttype {none=0,normal,desc};
        public sorttype list = sorttype.none;
        public KundeÜbersicht()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            Point openUpHere = new Point(352, 0);
            this.Location = openUpHere;
        }

        private void KundeÜbersicht_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        private void löschenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

        }

        public void Refresh()
        {
            oracle.Open();

            int counter = 0;
            listView1.Items.Clear();

            OracleDataReader r = oracle.Select("NAME, VORNAME, LAND, Nummer", "Kunden");
            while (r.Read())
            {
                listView1.Items.Add(r.GetValue(0).ToString());
                listView1.Items[counter].SubItems.Add(r.GetValue(1).ToString());
                listView1.Items[counter].SubItems.Add(r.GetValue(2).ToString());
                listView1.Items[counter].SubItems.Add(r.GetValue(3).ToString());
                counter++;
            }

            oracle.Close();            
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            KundeNeu kn = new KundeNeu();
            kn.MdiParent = this.MdiParent;
            kn.Show();
            Refresh();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                oracle.Open();
                oracle.Delete("Kunden", "Nummer=" + listView1.SelectedItems[0].SubItems[3].Text); oracle.Close();
                Refresh();
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            
             oracle.Open();
             string b = "ORDER BY Name";

             switch (list)
             {
                 case sorttype.none:
                     list = sorttype.normal;
                     break;
                 case sorttype.normal:
                     b += " DESC";
                     list = sorttype.desc;
                     break;
                 default:
                     b = "";
                     list = sorttype.none;
                     break;
             }
             OracleDataReader r = oracle.Select("NAME, VORNAME, LAND, NUMMER", "Kunden", "", b);

             int counter = 0;
             while (r.Read())
             {
                 listView1.Items.Add(r.GetValue(0).ToString());
                 listView1.Items[counter].SubItems.Add(r.GetValue(1).ToString());
                 listView1.Items[counter].SubItems.Add(r.GetValue(2).ToString());
                 listView1.Items[counter].SubItems.Add(r.GetValue(3).ToString());
                 counter++;
             }
             oracle.Close();
        }
    }
}
